package com.mca.framework.date;

/**
 * @Author: zhoupeng
 * @createTime: 2025年07月11日 11:04:30
 * @version: 1.0
 * @Description:
 * @copyright:
 */
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class DateFormatExample {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(100);

        // 使用线程安全的 DateTimeFormatter
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        for (int i = 0; i < 30; i++) {
            executorService.execute(() -> {
                for (int j = 0; j < 10; j++) {
                    // 每次都创建新的 LocalDateTime 实例，避免共享可变对象
                    LocalDateTime now = LocalDateTime.now();
                    String formatted = format(now, formatter);
                    LocalDateTime parsed = parse(formatted, formatter);
                    System.out.println(parsed);
                }
            });
        }

        executorService.shutdown();
    }

    // 使用线程安全的 DateTimeFormatter 进行格式化
    private static String format(LocalDateTime date, DateTimeFormatter formatter) {
        return date.format(formatter);
    }

    // 使用线程安全的 DateTimeFormatter 进行解析
    private static LocalDateTime parse(String dateString, DateTimeFormatter formatter) {
        return LocalDateTime.parse(dateString, formatter);
    }
}
